package com.huruwo.serialporthelper;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

import com.bjw.bean.ComBean;

import com.bjw.utils.SerialHelper;
import com.huruwo.serialporthelper.utils.SerialReadThread;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android_serialport_api.SerialPortFinder;


public class SerialPortHelperActivity extends AppCompatActivity {

    private RecyclerView     recy;
    private Spinner          spSerial;
    private EditText         edInput;
    private Button           btSend;
    private RadioGroup       radioGroup;
    private RadioButton      radioButton1;
    private RadioButton      radioButton2;
    private SerialPortFinder serialPortFinder;
    private SerialHelper     serialHelper;
    private Spinner          spBote;
    private Button           btOpen;
    private LogListAdapter   logListAdapter;
    private static final String TAG = "SerialPortHelper--->";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seria_port_helper);


        recy = (RecyclerView) findViewById(R.id.recy);

        edInput = (EditText) findViewById(R.id.ed_input);
        btSend = (Button) findViewById(R.id.bt_send);

        btOpen = (Button) findViewById(R.id.bt_open);


        logListAdapter = new LogListAdapter(null);

        recy.setLayoutManager(new LinearLayoutManager(this));
        recy.setAdapter(logListAdapter);
        recy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        iniview();
    }


    private void iniview() {
        serialPortFinder = new SerialPortFinder();

        serialHelper = new SerialHelper() {

            byte[] received = new byte[1024];
            int size;
            SerialReadThread st = new SerialReadThread() {
                @Override
                public void showText(final String text) {
                    runOnUiThread(new Runnable() {
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");

                        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                        String str = formatter.format(curDate);

                        @Override
                        public void run() {

                            logListAdapter.addData(str + " --> " + text);
                            recy.smoothScrollToPosition(logListAdapter.getData().size());
                        }
                    });
                }
            };

            @Override
            protected void onDataReceived(final ComBean comBean) {


                received = comBean.bRec;
                size = comBean.bRec.length;

                if (size > 0) {
                    st.onDataReceive(received, size);
                }


            }
        };


        //                            serialHelper.sendHex(edInput.getText().toString());
        //                            Log.e(TAG, "onClick: 发送十六进制: " + edInput.getText().toString());

        // serialHelper.close();
        try {
            //            serialHelper.setPort(Constants.PORT);
            serialHelper.setPort(Constants.PORT_bst);
            serialHelper.setBaudRate(Constants.BAUDRATE);
            serialHelper.open();
        } catch (IOException e) {

        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        serialHelper.close();
    }
}
